using System;
using System.Collections.Generic;
using System.Text;

namespace Xeml.Document
{
    /// <summary>
    /// Represent's a cycling value.
    /// </summary>
    public class Cycle : DynamicValueBase
    {
        #region Fields
        private TimeSpan length;
        private DynamicValueCollection values;
        
        #endregion

        #region Properties
        /// <summary>
        /// The length of the cycle.
        /// </summary>
        /// <remarks>Is calculated as the sum of the duration of eacvh cycle value.</remarks>
        public TimeSpan Length
        {
            get
            {
                TimeSpan ts = TimeSpan.Zero;
                foreach (DynamicValue v in this.Values)
                {
                    ts += v.Time;
                }
                return ts;
            }
        }


        /// <summary>
        /// A list of value within this cycle.
        /// </summary>
        /// The sum of the time property odf each item in this collection has to match the length property of this.
        public DynamicValueCollection Values
        {
            get
            {
                return this.values;
            }
        }

        #endregion

        #region Contructors
        /// <summary>
        /// The standard constructor.
        /// </summary>
        public Cycle()
        {
            this.values = new DynamicValueCollection();
            this.values.ItemAdded += new EventHandler<Mpi.Common.Collections.ItemEventArgs<DynamicValueBase>>(OnValueAdded);
            this.values.ItemRemoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<DynamicValueBase>>(OnValueRemoved);
            this.values.CollectionChanged += new EventHandler(OnValuesCollectionChanged);
        }

       

        /// <summary>
        /// The copy constructor.
        /// </summary>
        /// <param name="cycle">The prototype for the copy process.</param>
        public Cycle(Cycle cycle)
            : base(cycle)
        {
            this.values = new DynamicValueCollection();
            this.values.ItemAdded += new EventHandler<Mpi.Common.Collections.ItemEventArgs<DynamicValueBase>>(OnValueAdded);
            this.values.ItemRemoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<DynamicValueBase>>(OnValueRemoved);
            this.values.CollectionChanged += new EventHandler(OnValuesCollectionChanged);
       
            this.length = cycle.length;
            foreach (DynamicValue v in cycle.Values)
            {
                DynamicValue vnew = new DynamicValue(v);
                this.values.Add(vnew);
            }
        } 
        #endregion

        #region Methods


        private void OnValueRemoved(object sender, Mpi.Common.Collections.ItemEventArgs<DynamicValueBase> e)
        {
            (e.Item as DynamicValue).IsCycleValue = false;
        }

        private void OnValueAdded(object sender, Mpi.Common.Collections.ItemEventArgs<DynamicValueBase> e)
        {
            (e.Item as DynamicValue).IsCycleValue = true;
        }

        private void OnValuesCollectionChanged(object sender, EventArgs e)
        {

            OnPropertyChanged("Length");
            // OnChanged();

        }

        /// <summary>
        /// Converts the cycle to a string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("cycle@{0}", base.Time.ToString());
            return sb.ToString();
        }
        #endregion

       
    }
}
